package com.jianzh5.action.admin;

import com.google.common.collect.Maps;
import com.jianzh5.action.BaseAction;
import com.jianzh5.pojo.PAdmin;
import com.jianzh5.util.CacheUtils;
import com.jianzh5.util.InfoCenterHome;
import com.jianzh5.util.UserUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;


/**
 * Created by jianzh5 on 2015/7/10.
 */

@Controller
public class AdminAction extends BaseAction{

    @RequestMapping(value = "/admin",method = RequestMethod.GET)
    public String defalult(){
        logger.info("跳转登录页面...");
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            return "redirect:/admin/index";
        }
        return InfoCenterHome.SERVLET_ADMIN_ROOT+"/login";
    }

    @RequestMapping(value = "/admin" ,method = RequestMethod.POST)
    public String defalult(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String username,Model model){
        logger.info("执行登录操作...");
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            return "redirect:/admin/index";
        }else{
            model.addAttribute("isValidateCodeLogin", isValidateCodeLogin(username, true, false));
            return InfoCenterHome.SERVLET_ADMIN_ROOT+"/login";
        }
    }

    /**
     * 跳转到首页内容
     * @param model
     * @return
     */
    @RequestMapping("/admin/index")
    public String index(Model model){
        PAdmin adminUser = UserUtils.getUser();
        model.addAttribute("adminUser", adminUser);
        if(adminUser.getId() == null){
            return "redirect:/admin";
        }
        //登录成功，验证码计算器清零
        isValidateCodeLogin(adminUser.getUsername(), false, true);
        return InfoCenterHome.SERVLET_ADMIN_ROOT+"/index";
    }

    public static boolean isValidateCodeLogin(String username, boolean isFail, boolean clean){
        Map<String, Integer> loginFailMap = (Map<String, Integer>) CacheUtils.get("loginFailMap");
        if (loginFailMap==null){
            loginFailMap = Maps.newHashMap();
            CacheUtils.put("loginFailMap", loginFailMap);
        }
        Integer loginFailNum = loginFailMap.get(username);
        if (loginFailNum==null){
            loginFailNum = 0;
        }
        if (isFail){
            loginFailNum++;
            loginFailMap.put(username, loginFailNum);
        }
        if (clean){
            loginFailMap.remove(username);
        }
        return loginFailNum >= 3;
    }

    @RequestMapping("/logout")
    public String logout(Model model){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "/index";
    }

}
